<h1 id="数据库操作">数据库操作</h1>
<p><code>Quick</code>语言中，只需获取到<code>DB</code>对象即可进行数据库操作。  </p>
<h3 id="创建db的相关函数">创建DB的相关函数</h3>
<table>
<thead>
<tr>
<th>函数</th>
<th>描述</th>
</tr>
</thead>
<tbody><tr>
<td>sqlite</td>
<td>创建操作Sqlite的DB对象</td>
</tr>
<tr>
<td>mysql</td>
<td>创建操作Mysql的DB对象</td>
</tr>
<tr>
<td>sqlserver</td>
<td>创建操作SqlServer的DB对象</td>
</tr>
<tr>
<td>pg</td>
<td>创建操作Postgres的DB对象</td>
</tr>
<tr>
<td>oracle</td>
<td>创建操作Oracle的DB对象</td>
</tr>
</tbody></table>
<p>示例</p>
<pre><code class="hljs language-javascript"><span class="hljs-comment">// 数据库文件名</span>
db = <span class="hljs-title function_">sqlite</span>(<span class="hljs-string">&quot;d:/db3&quot;</span>)
<span class="hljs-comment">// 用户名， 密码，(ip/域名):端口/数据库名</span>
db = <span class="hljs-title function_">mysql</span>(<span class="hljs-string">&quot;root&quot;</span>, <span class="hljs-string">&quot;123456&quot;</span>, <span class="hljs-string">&quot;192.168.1.101:3306/iot&quot;</span>)
<span class="hljs-comment">// 用户名， 密码，(ip/域名):端口/数据库名</span>
db = <span class="hljs-title function_">sqlserver</span>(<span class="hljs-string">&quot;SA&quot;</span>, <span class="hljs-string">&quot;root@123&quot;</span>, <span class="hljs-string">&quot;192.168.1.101:1433/STG&quot;</span>)
<span class="hljs-comment">// 用户名， 密码，(ip/域名):端口/数据库名</span>
db = <span class="hljs-title function_">pg</span>(<span class="hljs-string">&quot;postgres&quot;</span>, <span class="hljs-string">&quot;123456&quot;</span>, <span class="hljs-string">&quot;192.168.1.101:5432/postgres?sslmode=disable&quot;</span>)
<span class="hljs-comment">// 用户名， 密码，(ip/域名):端口/数据库名</span>
db = <span class="hljs-title function_">oracle</span>(<span class="hljs-string">&quot;system&quot;</span>, <span class="hljs-string">&quot;oracle&quot;</span>, <span class="hljs-string">&quot;192.168.1.101:1521/xe&quot;</span>)
</code></pre>
<h3 id="db对象常用方法">db对象常用方法</h3>
<table>
<thead>
<tr>
<th>方法名</th>
<th>描述</th>
</tr>
</thead>
<tbody><tr>
<td>tables()</td>
<td>查找当前数据库的所有表名称</td>
</tr>
<tr>
<td>exists(tableName)</td>
<td>判断某个表是否存在</td>
</tr>
<tr>
<td>fields(tableName)</td>
<td>查找指定表的所有字段名称</td>
</tr>
<tr>
<td>count(tableName)</td>
<td>统计表的总记录数</td>
</tr>
<tr>
<td>exec(sql, args...)</td>
<td>执行建表，删表等sql</td>
</tr>
<tr>
<td>insert(sql, args...)</td>
<td>记录新增</td>
</tr>
<tr>
<td>update(sql, args...)</td>
<td>记录更新</td>
</tr>
<tr>
<td>getRows(sql, args...)</td>
<td>获取多行记录数据</td>
</tr>
<tr>
<td>getRow(sql, args...)</td>
<td>获取单行记录数据</td>
</tr>
<tr>
<td>getValue(sql, args...)</td>
<td>获取单个值。e.g. 查询记录数量</td>
</tr>
</tbody></table>
<h3 id="示例：">示例：</h3>
<pre><code class="hljs language-javascript">db = <span class="hljs-title function_">oracle</span>(<span class="hljs-string">&quot;system&quot;</span>, <span class="hljs-string">&quot;oracle&quot;</span>, <span class="hljs-string">&quot;127.0.0.1:1521/xe&quot;</span>)

studentTable = <span class="hljs-string">`
create table student (
id number,
name varchar(50),
age number
)
`</span>
db.<span class="hljs-title function_">exec</span>(studentTable)

rows = db.<span class="hljs-title function_">getRows</span>(<span class="hljs-string">&quot;select table_name from user_tables&quot;</span>)
forv row : rows {
  <span class="hljs-title function_">println</span>(<span class="hljs-string">&quot;name -&gt; &quot;</span>, row)
}
<span class="hljs-title function_">println</span>(<span class="hljs-string">&quot;total -&gt; &quot;</span>, rows.<span class="hljs-title function_">size</span>())

<span class="hljs-title function_">println</span>(<span class="hljs-string">&quot;--------------------&quot;</span>)

db.<span class="hljs-title function_">insert</span>(<span class="hljs-string">&quot;insert into student values(64, &#x27;lisi&#x27;, 123)&quot;</span>)
db.<span class="hljs-title function_">insert</span>(<span class="hljs-string">&quot;insert into student values(23, &#x27;zhangsan&#x27;, 56)&quot;</span>)
db.<span class="hljs-title function_">insert</span>(<span class="hljs-string">&quot;insert into student values(11, &#x27;changlie&#x27;, 9)&quot;</span>)

rows = db.<span class="hljs-title function_">getRows</span>(<span class="hljs-string">&quot;select * from student&quot;</span>)
forv row : rows {
  <span class="hljs-title function_">println</span>(<span class="hljs-string">&quot;stu =&gt; &quot;</span>, row)
}
</code></pre>
